package com.example.demo.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Admin
 * Date: 2023-03-11
 * Time: 11:44
 */
@RestController
@RequestMapping("/index")
@Slf4j
public class UserController {

    @RequestMapping("/login")
    public boolean login(HttpServletRequest request, String username, String password) {

        //判断代码是否为空
        if(StringUtils.hasLength("username") && StringUtils.hasLength("password")) {
            //判断账号密码是否正确
            if("admin".equals(username) && "admin".equals(password)) {
                HttpSession session = request.getSession(true);
                session.setAttribute("userinfo","admin");
                return true;
            }else {
                return false;
            }
        }

        return false;
    }

    //获取个人信息,登录过后才能访问
    @RequestMapping("/getinfo")
    public String getInfo() {
        log.debug("执行了 get info 方法!");
        return "执行了 get info 方法!";
    }

    //注册方法,不登录也能访问
    @RequestMapping("/reg")
    public String reg() {
        int num = 10 / 0;
        log.debug("执行了 reg 方法!");
        return "执行了 reg 方法!";
    }
}
